﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using SR.API;

namespace SR.UI.WebPart.NivoSlider.NivoSliderWebPart
{
    public enum SliderThemes { def, orman, pascal }
    public enum SliderTransitionEffects { sliceDown, sliceDownLeft, sliceUp, sliceUpLeft, sliceUpDown, sliceUpDownLeft, fold, fade, random, slideInLeft, slideInRight, boxRandom, boxRain, boxRainReverse, boxRainGrow, boxRainGrowReverse }
    [ToolboxItemAttribute(false)]
    public class NivoSliderWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/SR.UI.WebPart.NivoSlider/NivoSliderWebPart/NivoSliderWebPartUserControl.ascx";
        private uint count = 4;
        [Category("Image Slider Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("No of Images to display"), WebDescription("Specify the number of maximum images to be displyed in the slider, by default it is set to 4.")]
        public uint Count
        {
            get { return count; }
            set
            {
                count = value;
            }
        }

        private string siteUrl = "/";
        [Category("Image Slider Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Server relative web sitr url"), WebDescription("Server relative web sitr url, by default it is root site.")]
        public string SiteUrl
        {
            get { return siteUrl; }
            set
            {
                if (value.IsNullOrEmpty())
                    throw new Exception("Site url cannot be Empty!!");
                else
                    siteUrl = value;
            }
        }

        private string listName = "Images";
        [Category("Image Slider Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Image Library name"), WebDescription("Image Library name, by default it is 'Images'")]
        public string ListName
        {
            get { return listName; }
            set
            {
                if (value.IsNullOrEmpty())
                    throw new Exception("List Name cannot be Empty!!");
                else
                    listName = value;
            }
        }


        private SliderThemes sliderTheme = SliderThemes.def;
        [Category("Image Slider Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Nivo Slider Theme"), WebDescription("Nivo slider theme, by default it is 'default'")]
        public SliderThemes SliderTheme
        {
            get { return this.sliderTheme; }
            set
            {
                this.sliderTheme = value;
            }
        }

        private SliderTransitionEffects sliderTransitionEffect = SliderTransitionEffects.random;
        [Category("Image Slider Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Nivo Slider Transition Effect"), WebDescription("Nivo slider transition effect, by default it is 'random'")]
        public SliderTransitionEffects SlideTransition
        {
            get { return this.sliderTransitionEffect; }
            set { this.sliderTransitionEffect = value; }

        }

        private string linkUrl;
        [Category("Image Slider Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Column of target url"), WebDescription("Column url that points to the target")]
        public string LinkUrl
        {
            get { return this.linkUrl; }
            set { this.linkUrl = value; }

        }

        private bool skipJQuery=false;
        [Category("Image Slider Settings"), WebBrowsable, Personalizable(PersonalizationScope.Shared), WebDisplayName("Skip loading jquery which is included in webpart, default is false"), WebDescription("Skip loading jquery which is included in webpart. Check this property if you are loading jquery on masterpage/page layout")]
        public bool SkipJQuery
        {
            get { return this.skipJQuery; }
            set { this.skipJQuery = value; }

        }

        protected override void CreateChildControls()
        {
            try
            {
                NivoSliderWebPartUserControl control = (NivoSliderWebPartUserControl)Page.LoadControl(_ascxPath);
                control.parentControl = this;
                Controls.Add(control);
            }
            catch (Exception ex)
            {
                this.Controls.Clear();
                Controls.Add(new LiteralControl("<div><ul>" + ex.Message + "</ul><div/>"));
            }
        }
    }
}
